package city.spring.modules.oldman.service.impl;

import city.spring.modules.bed.service.BedManagementService;
import city.spring.modules.oldman.entity.OldmanDepositEntity;
import city.spring.modules.oldman.repository.OldmanDepositRepository;
import city.spring.modules.oldman.service.OldmanDepositService;
import city.spring.modules.oldman.service.OldmanInfoService;
import city.spring.modules.oldman.vo.OldmanDepositVO;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Service：老人押金
 *
 * @author 谢金婷
 * @date 2020-08-25 11:52:26
 */
@CacheConfig(cacheNames = {OldmanDepositServiceImpl.CACHE_NAME})
@Transactional(rollbackFor = Exception.class)
@Service
public class OldmanDepositServiceImpl extends ServiceImpl<OldmanDepositRepository, OldmanDepositEntity> implements OldmanDepositService {

    private final BedManagementService bedManagementService;
    private final OldmanDepositRepository oldmanDepositRepository;
    @Autowired
    OldmanInfoService oldmanInfoService;

    public OldmanDepositServiceImpl(BedManagementService bedManagementService, OldmanDepositRepository oldmanDepositRepository) {
        this.bedManagementService = bedManagementService;
        this.oldmanDepositRepository = oldmanDepositRepository;
    }


    @Override
    public void saveOldmanDeposit(OldmanDepositEntity entity) {
        if (!save(entity)) {
            throw new RuntimeException("保存信息失败");
        }
    }

    @Override
    public void updateOldmanDeposit(OldmanDepositEntity entity) {
        // 使用 updateById(entity); 修改数据时，将会修改 entity 对象中所有非null数据，如果某个字段为null，将会忽略该字段的修改
        if (!updateById(entity)) {
            throw new RuntimeException("修改信息失败");
        }
    }

    @Override
    public void deleteOldmanDeposit(String primaryKey) {
        removeById(primaryKey);
    }

    @Override
    public void deleteOldmanDeposit(List<String> primaryKeys) {
        removeByIds(primaryKeys);
    }

    @Override
    public IPage<OldmanDepositVO> listPage(Page<? extends OldmanDepositEntity> page, Wrapper<? extends OldmanDepositEntity> wrapper) {
        return baseMapper.listPage(page, wrapper);
    }

    @Override
    public List<OldmanDepositVO> getDepositByOldmanId(String oldmanId) {
        return oldmanDepositRepository.getDepositByOldmanId(oldmanId);
    }
}